package com.googlecode.monitoria.client.gui;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.util.Padding;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout.VBoxLayoutAlign;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayoutData;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.googlecode.monitoria.client.Application;
import com.googlecode.monitoria.client.beans.UserType;
import com.googlecode.monitoria.client.gui.assistant.AssistantTabPanel;
import com.googlecode.monitoria.client.gui.student.StudentTabPanel;
import com.googlecode.monitoria.client.gui.teacher.TeacherTabPanel;
import com.googlecode.monitoria.client.services.MonitoriaServiceAsync;
import com.googlecode.monitoria.client.to.UserTO;

/**
 * Default screen for a logged user.
 * @author Ricardo Araújo Santos - ricardo@lsd.ufcg.edu.br
 */
public class HomeScreen extends LayoutContainer {
	
	private final UserTO user;

	/**
	 * Default constructor.
	 * @param loggedUser User currently in session.
	 */
	public HomeScreen(UserTO loggedUser) {
		this.user = loggedUser;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setLayout(new BorderLayout());

		Button logoutButton = new Button("Logout");
		logoutButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				logout();
			}
		});
		
	    LayoutContainer north = new LayoutContainer();
	    VBoxLayout layout = new VBoxLayout();  
        layout.setPadding(new Padding(5));  
        layout.setVBoxLayoutAlign(VBoxLayoutAlign.RIGHT);  
        north.setLayout(layout);
        north.add(logoutButton, new VBoxLayoutData(new Margins(0)));
	    
	    BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 30);
	    add(north, northData);

	    ContentPanel west = new ContentPanel();  
	    west.setBodyBorder(false);  
	    west.setHeading("Options");  
	    west.setLayout(new AccordionLayout());
	    
	    BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 200, 100, 300);  
	    westData.setMargins(new Margins(5, 0, 5, 5));  
	    westData.setCollapsible(true);  

	    add(west, westData);  

	    
		TabPanel tabPanel = fillTabs();  

	    BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5));  
	    add(tabPanel, centerData);
	}

	private TabPanel fillTabs() {
		UserType userType = UserType.valueOf((String) user.get(UserTO.TYPE));
		switch (userType) {
		case TEACHER:
			return new TeacherTabPanel(user);
		case ASSISTANT:
			return new AssistantTabPanel(user);
		case STUDENT:
			return new StudentTabPanel(user);
		default:
			return null;
			// TODO show error panel or something... can a user not be one of those?
		}
	}
	
	/**
	 * Ends user session and go back to login screen. 
	 */
	protected void logout() {
		MonitoriaServiceAsync.Util.getInstance().logout(new AsyncCallback<Void>() {
			public void onSuccess(Void arg0) {
				Application.get().setLoginScreen();
			}
			
			public void onFailure(Throwable arg0) {
				Application.get().setLoginScreen();				
			}
		});
	}

}
